package com.admin.education.dataTransferObject.circles.Mapper;

import com.admin.education.dataTransferObject.circles.CirclesTransfer;
import com.admin.education.dataTransferObject.circles.Request.CreateCirclesRequest;
import com.admin.education.models.*;
import com.admin.education.repositories.UserInformationRepository;
import org.mapstruct.Mapper;
import org.mapstruct.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Mapper
public abstract class CirclesMapper {
    private QUserInformation qUserInformation;
    public CirclesMapper(){
        qUserInformation= QUserInformation.userInformation;
    }
    @Autowired
    private UserInformationRepository userInformationRepository ;

    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private AdmirersMapper admirersMapper;
    @Autowired
    private CommentsMapper commentsMapper;
    @Mappings({
            @Mapping(target="pictures",ignore=true),
//            @Mapping(target = "admirers",ignore = true),
            @Mapping(target = "comments",ignore = true)
    })
    public abstract CirclesTransfer mapFrom(Circles circles);
    @Mappings({
    })
    public abstract Circles mapFrom(CreateCirclesRequest createCirclesRequest);
    @Mappings({})
    public abstract List<CirclesTransfer> mapFrom(List<Circles> circles);
    @AfterMapping
    public void afterMapping(Circles circles,@MappingTarget CirclesTransfer circlesTransfer){
        if(null!=circles.getPirtures()){
            circlesTransfer.setPictures(this.pictureMapper.mapFrom(circles.getPirtures()));
        }
        UserInformation userInformation= this.userInformationRepository.selectFrom(qUserInformation).where(qUserInformation.id.eq(circles.getUserInformationId())).fetchFirst();
        if (null != userInformation) {
            circlesTransfer.setName(userInformation.getName());
            if (null != userInformation.getClasses()) {
                circlesTransfer.setClassName(userInformation.getClasses().getName());
            }
            if (null != userInformation.getSchools()) {
                circlesTransfer.setSchoolName(userInformation.getSchools().getName());
            }
            if (null != userInformation.getClasses().getGrades()) {
                circlesTransfer.setGradeName(userInformation.getClasses().getGrades().getName());
            }
        }
        if(null!=circles.getComments()){
            circlesTransfer.setComments(this.commentsMapper.mapFrom(circles.getComments()));
        }
//        if(null!=circles.getAdmirers()){
////            circlesTransfer.setAdmirers(this.admirersMapper.mapFrom(circles.getAdmirers().stream().collect(Collectors.toList())));
//            circlesTransfer.setAdmirerSize(circles.getAdmirers().size());
//        }
    }
    @AfterMapping
    public  void afterMapping(CreateCirclesRequest createCirclesRequest,@MappingTarget Circles circles){
        circles.setCreateTime(new Date());

        if(null!=createCirclesRequest.getPictures()&&0!=createCirclesRequest.getPictures().size()){
            circles.setPath(createCirclesRequest.getPictures().get(0));
            circles.setPirtures(new ArrayList<>());

            for (String path : createCirclesRequest.getPictures()) {
                Pictures pictures = new Pictures();

                pictures.setPath(path);
                pictures.setCircles(circles);

                circles.getPirtures().add(pictures);
            }

        }
    }
}
